from django.http import JsonResponse
from django.views.generic import View
from ..utils.literUtils import get_model_serializer_config, type_conversion_dict
from ..serializers.publicationInfoSerializer import PublicationCreateSerializer, BookCreateSerializer, JournalCreateSerializer, ConferenceCreateSerializer
from ..serializers.publicationListSerializer import JournalListQuerySerializer, JournalListSerializer, ConferenceListSerializer, ConferenceListQuerySerializer, BookListQuerySerializer, BookListSerializer

class PublicationConfigView(View):
    def get(self, request, publication_type):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            query_fields = []
            sortable_fields = []
            
            query_serializer = None
            config_serializer = None
            if publication_type == "Journal":
                query_serializer = JournalListQuerySerializer
                config_serializer = JournalListSerializer
                sortable_fields = ["name", "zh_name", "abbreviation", "impact_factor", "founding_datej", "publication_cycle"]
            elif publication_type == "Conference":
                query_serializer = ConferenceListQuerySerializer
                config_serializer = ConferenceListSerializer
                sortable_fields = ["name", "zh_name", "abbreviation", "founding_datec", "meeting_cycle"]
            elif publication_type == "Book":
                query_serializer = BookListQuerySerializer
                config_serializer = BookListSerializer
                sortable_fields = ["name", "zh_name", "abbreviation", "publish_date", "language"]
            else:
                res["code"] = 500
                res["message"] = "出版物类型不存在"
                return JsonResponse(res)
            
            query_fields = query_serializer.get_query_fields()
            for field in query_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["query_fields"] = query_fields
            table_fields = []
            table_fields = get_model_serializer_config(config_serializer)
            table_fields.append({"title":"操作","dataIndex":"operate","type":"operate"})
            hidden_fields = ['uuid']
            for field in table_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in sortable_fields:
                    field["sortable"] = True
            # 获取展示字段
            data["table_fields"] = table_fields
            res["code"] = 200
            res["data"] = data
            res["message"] = "publication配置获取成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)

class PublicationAddConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            add_fields = []
            add_fields = get_model_serializer_config(PublicationCreateSerializer)
            book_fields = get_model_serializer_config(BookCreateSerializer)
            jouranl_fields = get_model_serializer_config(JournalCreateSerializer)
            conference_fields = get_model_serializer_config(ConferenceCreateSerializer)
            
            for f in book_fields:
                f['parentClasses'] = ['Book']
                add_fields.append(f)
                
            for f in jouranl_fields:
                f['parentClasses'] = ['Journal']
                add_fields.append(f)
                
            for f in conference_fields:
                f['parentClasses'] = ['Conference']
                add_fields.append(f)
            
            hidden_fields = []
            cit_hidden_fields = ['subjects', 'institutions', 'departments', 'authors']
            for field in add_fields:
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in cit_hidden_fields:
                    field["citHidden"] = True
                field["type"] = type_conversion_dict[field["type"]]
            # extra_fields_info = PublicationCreateSerializer.get_extra_fields()
            # for extra_fields  in extra_fields_info:
            #     add_fields.append(extra_fields)
            for field in add_fields:
                if field["type"] == "foreignkey" or field["type"] == 'relation' or field["type"] == 'bool':
                    field["required"] = False
            data["create_fields"] = add_fields
            res["code"] = 200
            res["data"] = data
            res["message"] = "成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)